using System;
using System.Collections.Generic;
using DG.Tweening;
using LitJson;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelWalletDetail : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private float menuClose = 0;
        private float menuOpen = 148;
        private float scrollClose = 155;
        private float scrollOpen = 318;
        private string startDate, endDate;
        private string[] valueColors =
        {
            "#5B7EFE", //充值
            "#FF9A23", //提现
            "#9457FB", //租用
            "#00D89B", //收益
            "#FE5758", //返利
            "#24A6CF", //推广
            "#E8D56A", //退款
        };

        public static void Open()
        {
            UIManagerComponent.instance.OpenUI<PanelWalletDetail>(UILayer.Normal, RootComponent.instance, false);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelWalletDetail>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelWalletDetail", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            Menu.gameObject.SetActive(false);
            Vector2 offset = ScrollView.rectTransform.offsetMax;
            offset.y = -scrollClose;
            ScrollView.rectTransform.offsetMax = offset;

            string api = "/v1/user/wallet/record";
            string listKey = "Results";
            Table.Column[] columns =
            {
                (cell, data) => TimeHelper.Parse2Date(data["created_at"].ToString(),false),
                (cell, data) => TimeHelper.Parse2Time(data["created_at"].ToString(), false),
                (cell, data) =>
                {
                    Image img = cell.Get<Image>("Icon");
                    img.sprite = cell.Get<GameObject>("Icon").Get<Sprite>((int)data["type"] - 1);
                    cell.Get<Text>("Value2").color = ColorHelper.Color(valueColors[(int)data["type"] - 1]);
                    img.SetNativeSize();
                    return Menu.switchs[(int)data["type"]].text.text;
                },
                (cell, data) => $"<color={Config.NumColor}>{I18N.Currency}<b>{StringHelper.Format2Decimal((int)data["amount"] * 0.01f)}</b></color>",
            };
            startDate = DateTime.Now.ToString("yyyy-MM-01");
            endDate = DateTime.Now.ToString("yyyy-MM-dd");
            Table.GetComponent<Table>().ReqData(api, listKey, columns, startDate: startDate, endDate: endDate);

            Switch.triggerInterval = 0.2f;
            Switch.SetClick(() =>
            {
                if (!Menu.gameObject.activeSelf)
                {
                    OpenMenu();
                }
                else
                {
                    CloseMenu();
                }
            });

            Switch.text.text = Menu.switchs[0].text.text;
            Menu.onSwitchChange = index =>
            {
                Switch.text.text = Menu.switchs[index].text.text;
                string _api = index == 0 ? api : api + "?type=" + index;
                Table.GetComponent<Table>().ReqData(_api, listKey, columns, startDate: startDate, endDate: endDate);
            };

            FromDate.SetClick(() =>
            {
                ScrollView.enabled = false;
                DatePicker.Open(dateTime =>
                {
                    startDate = dateTime;
                    RefreshDateUI();
                    string _api = Menu.CurrIndex == 0 ? api : api + "?type=" + Menu.CurrIndex;
                    Table.GetComponent<Table>().ReqData(_api, listKey, columns, startDate: startDate, endDate: endDate);
                }, () =>
                {
                    ScrollView.enabled = true;
                });
            });

            ToDate.SetClick(() =>
            {
                ScrollView.enabled = false;
                DatePicker.Open(dateTime =>
                {
                    endDate = dateTime;
                    RefreshDateUI();
                    string _api = Menu.CurrIndex == 0 ? api : api + "?type=" + Menu.CurrIndex;
                    Table.GetComponent<Table>().ReqData(_api, listKey, columns, startDate: startDate, endDate: endDate);
                }, () =>
                {
                    ScrollView.enabled = true;
                });
            });

            RefreshDateUI();
        }

        private void RefreshDateUI()
        {
            FromDate.text.text = $"<color=#999999>From:</color> {TimeHelper.Parse2Date(startDate, true)}";
            ToDate.text.text = $"<color=#999999>To:</color> {TimeHelper.Parse2Date(endDate, true)}";
        }

        private void OpenMenu()
        {
            Menu.gameObject.SetActive(true);
            float duration = Switch.triggerInterval;
            DOVirtual.Float(menuClose, menuOpen, duration, v =>
            {
                Menu.rectTransform.sizeDelta = new Vector2(Menu.rectTransform.sizeDelta.x, v);
            });
            DOVirtual.Float(scrollClose, scrollOpen, duration, v =>
            {
                Vector2 offset = ScrollView.rectTransform.offsetMax;
                offset.y = -v;
                ScrollView.rectTransform.offsetMax = offset;
            });
        }

        private void CloseMenu()
        {
            float duration = 0.2f;
            DOVirtual.Float(menuOpen, menuClose, duration, v =>
            {
                Menu.rectTransform.sizeDelta = new Vector2(Menu.rectTransform.sizeDelta.x, v);
            }).OnComplete(() =>
            {
                Menu.gameObject.SetActive(false);
            });
            DOVirtual.Float(scrollOpen, scrollClose, duration, v =>
            {
                Vector2 offset = ScrollView.rectTransform.offsetMax;
                offset.y = -v;
                ScrollView.rectTransform.offsetMax = offset;
            });
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
